package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.DstChannelCfgMapper;
import cc.rengu.igas.bomp.common.entity.DstChannelCfg;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 支付通道配置操作表操作接口实现类
 * Created by 王成 on 2020/2/10
 */
public class DstChannelCfgMapperImpl implements DstChannelCfgMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);


    @Override
    public List<DstChannelCfg> selectBompDstChannelCfgByDstChannelId(String instId, String dstChannelId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompDstChannelCfgByDstChannelId");
        DstChannelCfg dstChannelCfg = new DstChannelCfg();
        dstChannelCfg.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(dstChannelId)) {
            dstChannelCfg.setDstChannelId(dstChannelId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, dstChannelCfg, DstChannelCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据通道标识查询支付通道配置失败,instId:<{}>,dstChannelId:<{}>", instId, dstChannelId);
            return null;
        }
        return objectList.stream().map(item -> (DstChannelCfg) (item)).collect(Collectors.toList());
    }

    @Override
    public List<DstChannelCfg> selectBompUpDstChannelCfgByDstChannelType(String instId, String dstChannelType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompUpDstChannelCfgByDstChannelType");
        DstChannelCfg dstChannelCfg = new DstChannelCfg();
        dstChannelCfg.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(dstChannelType)) {
            dstChannelCfg.setDstChannelType(dstChannelType);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, dstChannelCfg, DstChannelCfg.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据通道标识查询支付通道配置失败,instId:<{}>,dstChannelType:<{}>", instId, dstChannelType);
            return null;
        }
        return objectList.stream().map(item -> (DstChannelCfg) (item)).collect(Collectors.toList());
    }
}
